﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNotebook;
using DevExpress.XtraBars.Ribbon;
namespace NotebookS2
{
    public partial class M_DEPT : RibbonForm
    {
        public M_DEPT()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public DataTable Departments { get; set; }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (txtDepartment.Text.Trim() != "")
            {
                DepartmentManager oManager = new DepartmentManager();
                oManager.ConnectionString = this.ConnectionString;

                oManager.Open();

                if (oManager.IsDepartmentExists(txtDepartment.Text) == false)
                {
                    oManager.AddDepartment(txtDepartment.Text);


                    this.Departments = oManager.GetDepartment();
                }
                else
                {
                    MessageBox.Show("Department already exists!!!", "Department", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtDepartment.Focus();
                    txtDepartment.SelectAll();
                }
                oManager.Close();


                gridControl1.DataSource = this.Departments;
                gridControl1.Refresh();
            }
            else
            {
                MessageBox.Show("Department name should not be blank!!!", "Department", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtDepartment.Focus();
            }

        }



        void BindDepartment()
        {
            DepartmentManager oManager = new DepartmentManager();
            oManager.ConnectionString = this.ConnectionString;

            oManager.Open();

            this.Departments = oManager.GetDepartment();
            gridControl1.DataSource = this.Departments;
            gridControl1.Refresh();

            oManager.Close();

        }
        private void M_DEPT_Load(object sender, EventArgs e)
        {
            BindDepartment();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DepartmentManager oManager = new DepartmentManager();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            oManager.UpdateDepartment(this.Departments);

            oManager.Close();

            MessageBox.Show("Department is already updated!!!", "Department", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
